package com.qf.web;

import com.qf.constaint.User_Containt;
import com.qf.others.MyConvert;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.serviceimpl.UserServiceImpl;
import com.qf.utils.MD5Utils;
import com.qf.utils.UUIDUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;
@SuppressWarnings("all")
@WebServlet("/user")
public class UserServlet extends BaseServlet {
    /**
     * 校验用户名
     */
    public void checkName(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        //String methodName = request.getParameter("methodName");
        //System.out.println(methodName);
        //System.out.println(username);
        UserService us = new UserServiceImpl();
        boolean flag = us.checkUser(username);
        if(flag){
            response.getWriter().write("1");
        }else{
            response.getWriter().write("0");
        }
    }
    /**
     *校验验证码,注册用户
     */
    public void registerUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");//接收前台传来的验证码
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");//接收后台存储的验证码
        session.removeAttribute("CHECKCODE_SERVER");//移除信息
        if(code == null || !code.equalsIgnoreCase(checkcode_server)){
            request.setAttribute("message","验证码错误");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
            return;
        }
        User user = null;
        //封装User实体
        try {
            Map<String, String[]> map = request.getParameterMap();
            //处理日期格式
            ConvertUtils.register(new MyConvert(), Date.class);
            user = new User();
            BeanUtils.populate(user,map);//将数据封装进user实体
            //封装其他数据
            String uid = UUIDUtils.getId();//获取uid
            user.setUid(uid);
            String password = MD5Utils.md5(user.getPassword());//加密密码
            user.setPassword(password);
            String code1 = UUIDUtils.getCode();//获取随机激活码
            user.setCode(code1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //System.out.println(user);
        UserService us = new UserServiceImpl();
        us.registerUser(user);
        request.setAttribute("message","用户注册成功,点击激活!");
        request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
    }
    /**
     *激活用户
     */
    public void activeUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        UserService us = new UserServiceImpl();
        User user = us.findUserByCode(code);
        if(user != null){
            request.setAttribute("message","激活成功,请<a href='http://localhost:8080/maven_study_war/jsp/login.jsp'>登录</a>");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
        }else{
            request.setAttribute("message","激活失败,请重新激活");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
        }
    }
    /**
     *用户登录
     */
    public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String yanzheng = request.getParameter("yanzheng");//获取输入的验证码
        //System.out.println(yanzheng);
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");
        if(!checkcode_server.equalsIgnoreCase(yanzheng) || yanzheng == null){
            request.setAttribute("message","验证码错误!");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
            return;
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        password = MD5Utils.md5(password);
        UserService us = new UserServiceImpl();
        User user = us.loginUser(username,password);
        if(user == null){//用户不存在
            request.setAttribute("message","用户名或密码错误");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
            return;
        }
        if(User_Containt.user_Containt!=user.getState()){
            request.setAttribute("message","用户尚未激活,请<a href='http://localhost:8080/maven_study_war/user?methodName=activeUser&code=+user.getCode()+'>激活</a>");
            request.getRequestDispatcher("/jsp/message.jsp").forward(request,response);
            return;
        }
        session.setAttribute("user",user);
        /*request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);*/
        /**
         *自动登录
         */
        String autologin = request.getParameter("autologin");//接受用户是否勾选了自动提交
        if(!"autoLogin".equals(autologin)){//没有勾选
            Cookie cookie = new Cookie("autolg","");
            cookie.setMaxAge(0);//删除cookie
            response.addCookie(cookie);
        }else{//勾选了
            String content = user.getUsername()+":"+user.getPassword();
            content = URLEncoder.encode(content);
            Cookie cookie = new Cookie("autolg",content);
            cookie.setMaxAge(60*60*24*60);
            response.addCookie(cookie);
        }
        response.sendRedirect(request.getContextPath()+"/");//重定向到首页
    }

}
